﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Rendering;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers.Order
{
    public class Site_Payment_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Order/site_payment_edit.cshtml";
        private string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected Site_payment model = new Site_payment();
        public IPaymentService _payment { get; set; }
        public ISite_paymentService _site_payment { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("order_payment", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            string _action = Request.Query["action"];

            if (!string.IsNullOrEmpty(_action) && _action == CMSEnums.ActionEnum.Edit.ToString())
            {
                this.action = CMSEnums.ActionEnum.Edit.ToString();//修改类型
                int.TryParse(Request.Query["id"], out this.id);
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                    return;
                }
                if (!_site_payment.Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    filterContext.Result = result;
                    return;
                }
            }
            //赋值实体
            this.model = _site_payment.GetModel(this.id);
            if (this.model == null)
                this.model = new Site_payment();
            ViewBag.Id = this.id.ToString();
            ViewBag.Action = this.action;
        }
        //
        // GET: /admin/Payment_Edit/

        public ActionResult Index()
        {
            ViewData["model"] = this.model;            
            ShowInfo(this.id);
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ActionResult result = View(EDIT_RESULT_VIEW);
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("order_payment", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return result;
                }
                JscriptMsg("修改站点支付方式成功！", "../site_payment_list/index");
            }
            else //添加
            {
                ChkAdminLevel("order_payment", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return result;
                }
                JscriptMsg("添加站点支付方式成功！", "../site_payment_list/index");
            }
            return result;
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {            
            ShowFields(model.payment_id);
        }
        #endregion

        #region 显示相关字段=============================
        private void ShowFields(int _payment_id)
        {            
            var pays = _payment.GetList(model.payment_id);
            var payModel = _payment.GetModel(model.payment_id);
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "请选择平台...", Value = "" });
            foreach (var dr in pays)
            {
                list.Add(new SelectListItem() { Text = dr.title, Value = dr.id.ToString() });
            }
            ViewData["list"] = list;
            string api_path = "";
            if (payModel != null)
            {
                if (!string.IsNullOrEmpty(payModel.api_path))
                {
                    api_path = payModel.api_path;
                }
            }
            switch (api_path.ToLower()) {
               case "alipaypc":
                  ViewBag.div_key1_container = true;
                  ViewBag.div_key1_title = "支付宝账号";
                  ViewBag.div_key1_tip = "*签约支付宝账号或卖家支付宝帐户";
                  ViewBag.div_key2_container = true;
                  ViewBag.div_key2_title = "合作者身份(partner ID)";
                  ViewBag.div_key2_tip = "*合作身份者ID，以2088开头由16位纯数字组成的字符串";
                  ViewBag.div_key3_container = true;
                  ViewBag.div_key3_title = "交易安全校验码(key)";
                  ViewBag.div_key3_tip = "*交易安全检验码，由数字和字母组成的32位字符串";
                  ViewBag.div_key4_container = false;
                  ViewBag.txtKey4 = string.Empty;
                  break;
               case "alipaymb":
                  goto case "alipaypc";
               case "tenpaypc":
                  ViewBag.div_key1_container = true;
                  ViewBag.div_key1_title = "财付通商户号";
                  ViewBag.div_key1_tip = "*财付通商家服务商户号";
                  ViewBag.div_key2_container = true;
                  ViewBag.div_key2_title = "财付通密钥";
                  ViewBag.div_key2_tip = "*财付通商家服务密钥";
                  ViewBag.div_key3_container = false;
                  ViewBag.txtKey3 = string.Empty;
                  ViewBag.div_key4_container = false;
                  ViewBag.txtKey4 = string.Empty;
                  break;
               case "chinabankpc":
                  ViewBag.div_key1_container = true;
                  ViewBag.div_key1_title = "商户编号";
                  ViewBag.div_key1_tip = "*网银在线商户编号";
                  ViewBag.div_key2_container = true;
                  ViewBag.div_key2_title = "MD5校验码";
                  ViewBag.div_key2_tip = "*网银在线MD5校验码";
                  ViewBag.div_key3_container = false;
                  ViewBag.txtKey3 = string.Empty;
                  ViewBag.div_key4_container = false;
                  ViewBag.txtKey4 = string.Empty;
                  break;
               case "wxapipay":
                  ViewBag.div_key1_container = true;
                  ViewBag.div_key1_title = "商户号";
                  ViewBag.div_key1_tip = "*微信支付的商户号";
                  ViewBag.div_key2_container = true;
                  ViewBag.div_key2_title = "支付密钥";
                  ViewBag.div_key2_tip = "*商户支付密钥，进入微信支付【账户设置】【API安全】设置";
                  ViewBag.div_key3_container = true;
                  ViewBag.div_key3_title = "AppID";
                  ViewBag.div_key3_tip = "*微信公众号的AppID(应用ID)";
                  ViewBag.div_key4_container = true;
                  ViewBag.div_key4_title = "AppSecret";
                  ViewBag.div_key4_tip = "*微信公众号的AppSecret(应用密钥)";
                  break;
               case "wxnatpay":
                  ViewBag.div_key1_container = true;
                  ViewBag.div_key1_title = "商户号";
                  ViewBag.div_key1_tip = "*微信支付的商户号";
                  ViewBag.div_key2_container = true;
                  ViewBag.div_key2_title = "支付密钥";
                  ViewBag.div_key2_tip = "*商户支付密钥，进入微信支付【账户设置】【API安全】设置";
                  ViewBag.div_key3_container = true;
                  ViewBag.div_key3_title = "AppID";
                  ViewBag.div_key3_tip = "*微信公众号的AppID(应用ID)";
                  ViewBag.div_key4_container = false;
                  ViewBag.txtKey4 = string.Empty;
                  break;
               default:
                  ViewBag.div_key1_container = false;
                  ViewBag.div_key2_container = false;
                  ViewBag.div_key3_container = false;
                  ViewBag.div_key4_container = false;
                  ViewBag.txtKey3 = string.Empty;
                  ViewBag.txtKey2 = string.Empty;
                  ViewBag.txtKey1 = string.Empty;
                  ViewBag.txtKey4 = string.Empty;
                  break;
            }
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            this.model = new Site_payment();
            model.payment_id = CMSRequest.GetFormInt("ddlPaymentId", 0, HttpContext);
            model.sort_id = CMSRequest.GetFormInt("txtSortId", 0, HttpContext);
            model.title = CMSRequest.GetFormString("txtTitle", HttpContext);
            model.key1 = CMSRequest.GetFormString("txtKey1", HttpContext);
            model.key2 = CMSRequest.GetFormString("txtKey2", HttpContext);
            model.key3 = CMSRequest.GetFormString("txtKey3", HttpContext);
            model.key4 = CMSRequest.GetFormString("txtKey4", HttpContext);
            if (_site_payment.Add(model) > 0)
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "添加站点支付方式:" + model.title); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            model.payment_id = CMSRequest.GetFormInt("ddlPaymentId", 0, HttpContext);
            model.sort_id = CMSRequest.GetFormInt("txtSortId", 0, HttpContext);
            model.title = CMSRequest.GetFormString("txtTitle", HttpContext);
            model.key1 = CMSRequest.GetFormString("txtKey1", HttpContext);
            model.key2 = CMSRequest.GetFormString("txtKey2", HttpContext);
            model.key3 = CMSRequest.GetFormString("txtKey3", HttpContext);
            model.key4 = CMSRequest.GetFormString("txtKey4", HttpContext);
            if (_site_payment.Update(model)) {
               AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改站点支付方式:" + model.title); //记录日志
               result = true;
            }            
            return result;
        }
        #endregion
    }
}
